package com.xts.weinfo.model;

import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.exceptions.HyphenateException;
import com.xts.weinfo.base.BaseModel;
import com.xts.weinfo.base.Constants;
import com.xts.weinfo.bean.Bean;
import com.xts.weinfo.bean.Meizi;
import com.xts.weinfo.net.ApiService;
import com.xts.weinfo.net.BaseSubscriber;
import com.xts.weinfo.net.HttpUtil;
import com.xts.weinfo.net.ResultCallBack;
import com.xts.weinfo.net.RxUtils;
import com.xts.weinfo.util.LogUtil;
import com.xts.weinfo.util.ThreadManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 封装网络框架和M层
 * 1.重复代码太多(Retrofit和ApiService有一个对象就行了,不用每次都new)
 * 2.如果网络请求正在进行,用户退出了当前界面,一会数据回来更新ui的时候会报错
 * 解决方案:界面销毁的时候取消订阅关系(onDestory)
 * 3.封装观察者ResourceSubscriber
 */
public class MainModel extends BaseModel {

    /*  *//**
     * 获取好友列表
     *//*
    private Map<String, EaseUser> getContacts(){
        try {
            List<String> usernames = EMClient.getInstance()
                    .contactManager().getAllContactsFromServer();
        } catch (HyphenateException e) {
            e.printStackTrace();
        }
        Map<String, EaseUser> contacts = new HashMap<String, EaseUser>();
        for(int i = 1; i <= 10; i++){
            EaseUser user = new EaseUser("easeuitest" + i);
            contacts.put("easeuitest" + i, user);
        }
        return contacts;
    }*/

    ////获取联系人+群组
    public void getContactList(final ResultCallBack<Map<String, EaseUser>> callBack) {
        ThreadManager.getInstance()
                .execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            //获取联系人
                            List<String> usernames = EMClient.getInstance().contactManager()
                                    .getAllContactsFromServer();

                            //获取群组
                            //从服务器获取自己加入的和创建的群组列表，此api获取的群组sdk会自动保存到内存和db。
                            List<EMGroup> grouplist = EMClient.getInstance().
                                    groupManager().getJoinedGroupsFromServer();//需异步处理


                            Map<String, EaseUser> contacts = new HashMap<String, EaseUser>();
                            //添加联系人
                            for(int i = 0; i < usernames.size(); i++){
                                String username = usernames.get(i);
                                EaseUser user = new EaseUser(username);
                                //单聊
                                user.setChatType(EaseConstant.CHATTYPE_SINGLE);
                                contacts.put(username,user);
                            }

                            //添加群组
                            for (int i = 0; i < grouplist.size(); i++) {
                                String  name= grouplist.get(i).getGroupName();
                                String  id= grouplist.get(i).getGroupId();
                                EaseUser easeUser = new EaseUser(id);
                                //群聊
                                easeUser.setChatType(EaseConstant.CHATTYPE_GROUP);
                                easeUser.setGroupId(id);
                                contacts.put(id,easeUser);
                            }

                            callBack.onSuccess(contacts);
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                            callBack.onFail(e.toString());
                        }
                    }
                });
    }
}
